#include "chooseleavewidget.h"
#include <QPainter>
#include "mybutton.h"
#include <QGridLayout>
#include <QLabel>
#include <QDebug>
#include <QEvent>
#include <QTimer>
#include <QMainWindow>
#include "mainscence.h"
#include "startwidget.h"
#include <QSound>

ChooseLeaveWidget::ChooseLeaveWidget(QWidget *parent) : QWidget(parent){
    this->setFixedSize(320, 565);
    MyButton * backBtn = new MyButton(":/resource/BackButton.png",":/resource/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(240, 531);
    //返回按钮音效
    QSound * backSound = new QSound(":/resource/BackButtonSound.wav",this);
//    选择关卡按钮音效
    QSound *chosesceneSound=new QSound(":/resource/TapButtonSound.wav",this);
    connect(backBtn, &MyButton::clicked, [=](){
       backSound->play();
       QTimer::singleShot(160,this,[=](){
            emit sendBackToStartpane();
       });
    });

    //用来存放关卡按钮 使用格栅布局
    QWidget * levelIconPane = new QWidget(this);
    levelIconPane->move(28,130);
    QGridLayout * layout = new QGridLayout(this);

//    画关卡按钮 放入布局
    for (int i = 0; i<20 ; i++ ) {
        MyButton * btn = new MyButton(":/resource/LevelIcon.png");
        layout->addWidget(btn, i/4, i%4,1,1);
        connect(btn,&MyButton::clicked, [=](){
            qDebug() << "选择了第 " <<i+1 <<"个关卡;";
            chosesceneSound->play();

            GameWidget * gw = new GameWidget(i+1);
            QTimer::singleShot(160,this,[=](){
                emit sendGotoGamepane(gw);
            });

        });
        //指定parent为btn
        QLabel * label = new QLabel(QString::number(i+1),btn);
        label->setFixedSize(btn->width(),btn->height());
        //设置居中
        label->setAlignment(Qt::AlignCenter | Qt::AlignVCenter);

//        QLabel * label = new QLabel(QString::number(i+1),this);
//        label->move(59+(i%4)*62.6,160+(i/4)*62.8);

        //鼠标穿透
        label->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
    levelIconPane->setLayout(layout);

}


//事件过滤器
//installEventFilter(this)
//重写事件过滤器
//bool ChooseLeaveWidget::eventFilter(QObject * obj, QEvent * ev){
//    if(dynamic_cast<QLabel *>(obj) != NULL){
//        if (ev->type() == QEvent::MouseButtonPress){
//            qDebug() << "鼠标按在QLabel上";
//            return true;
//        }
//    }
//    return QWidget::eventFilter(obj,ev);
//}

void ChooseLeaveWidget::paintEvent(QPaintEvent *){

    QPainter painter(this);
    QPixmap pix;
    pix.load(":/resource/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/resource/Title.png");
    pix.scaled(pix.width()*1.7,pix.height()*1.7);
    painter.drawPixmap(this->width()*0.5-pix.width()*0.5,5,pix.width(),pix.height(),pix);

}
